from django.http import HttpResponse,HttpResponseForbidden
from django.middleware.csrf import get_token
from django.utils.deprecation import MiddlewareMixin

def my_middleware1(get_response):
    print('my_middleware1 init called')

    def middleware(request):
        print('my_middleware1 called before request')
        response = get_response(request)
        # print(response)
        print('my_middleware1 called after request')
        return response
    return middleware

def my_middleware2(get_response):
    print('my_middleware2 init called')

    def middleware(request):
        print('my_middleware2 called before request')
        response = get_response(request)
        print('my_middleware2 called after request')
        return response
    return middleware

class MyMiddleware1(MiddlewareMixin):
    print('3')
    def process_request(self,request):
        print('M1,request')
        user_agent = request.META.get('HTTP_USER_AGENT')
        print('M1 end')
        if not user_agent or not user_agent.lower().startswith('mozilla'):
            return HttpResponseForbidden('爬虫请自觉...')




class MyMiddleware(MiddlewareMixin):
    def process_request(self, request):
        csrf_token = get_token(request)
